import { createAction } from 'redux-actions'
import { getToken, check, body, delJson, getJson, postJson, putJson, parseJSON, checkStatus, data } from './util'
import util from '../util'

export
const fetchOnboardingTemplate = createAction('fetch onboarding template', onboardingTemplateId =>
  fetch(`/onboarding-templates/${onboardingTemplateId}`, getJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
		.then(data)
)

export 
const fetchOnboardingTemplates = createAction('fetch onboarding templates', () =>
  fetch('/onboarding-templates', getJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
		.then(data)
)

export
const addOnboardingDocument = createAction('add onboarding document')

export
const deleteOnboardingDocument = createAction('delete onboarding document')

export
const sortOnboardingDocument = createAction('sort onboarding document')

export
const updateOnboardingTempate = createAction('update onboarding template', (arg, onboardingTemplateId, cb) =>
  fetch(`/onboarding-templates/${onboardingTemplateId}`, {
    ...putJson,
    ...body(arg)
  })
  .then(checkStatus)
  .then(parseJSON)
  .then(check)
  .then(cb)
)

export
const fetchDocuments = createAction('fetch documents', () =>
  fetch('/documents', getJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
		.then(data)
)

export
const downloadDocument = createAction('download document', (documentId, cb) =>
	fetch(`/documents/${documentId}/download`, getJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
		.then(cb)
)

export
const deleteDocument = createAction('delete document', ({ documentId }, cb) =>
  fetch(`/documents/${documentId}`, delJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
    .then(() => {
      cb && cb()
      return documentId
    })
)

export
const createDocument = createAction('create document', (arg, cb) =>
  fetch('/documents', {
    ...postJson,
    ...body(arg)
  })
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
		.then(res => {
			cb && cb(res.data)
			return res.data
		})
)

export
const addOnboardingTemplate = createAction('add onboarding template', (arg, cb) =>
  fetch('/onboarding-templates', {
    ...postJson,
    ...body(arg)
  })
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
		.then(cb)
)

export
const deleteOnboardingTemplate = createAction('delete onboarding template', ({ onboardingTemplateId }, cb) =>
  fetch(`/onboarding-templates/${onboardingTemplateId}`, delJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
		.then(cb)
)

export
const resetOnboardingTemplates = createAction('reset onboarding templates')

export
const saveUserProfile = createAction('save user profile', (fields, callback) =>
  fetch(`/users/profiles/fields`, {
    ...putJson,
    ...body(fields)
  })
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
		.then(res => {
			callback && callback()
			return fields
		})
)

export
const createOnboardingFlow = createAction('create onboarding flow', (arg, cb) =>
  fetch('/onboarding-flows/hr', {
    ...postJson,
    ...body(arg)
  })
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
		.then(() => cb)
)

export
const fetchInvitationUrl = createAction('fetch invitation url', userId =>
  fetch(`/onboarding-flows/hr/${userId}/invitation-url`, getJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
    .then(data)
)

export
const fetchOnboardingFlows = createAction('fetch onboarding flows', (arg,cb) =>
  fetch(`/onboarding-flows/hr?${util.body(arg)}`, getJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
    .then(res => {
      cb && cb()
      return res.data
    })
)


export
const updateOnboardingFlowStatus = createAction('update onboarding flow status', (userId, onboardingStatus, cb) =>
  fetch(`/onboarding-flows/hr/${userId}/status`, {
    ...putJson,
    ...body({ onboardingStatus })
  })
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
    .then(cb)
)

export
const fetchRole = createAction('fetch role', userId =>
  fetch(`/roles/set-up-roles?${util.body({ userId })}`, getJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
    .then(res => {
      return res.data.roles
    })
)

export 
const resetOnboardingTemplate = createAction('reset onboarding template')

export
const uploadProfile = createAction('_', (profileTemplateId, file, cb) => {
  //var data = new FormData()
  //data.append('file', file)
  return (
    fetch(`/onboarding-flows/profile-templates-csv-files/upload/${profileTemplateId}`, {
      ...postJson,
      headers: {
        ...postJson.headers,
      },
      body: file
    })
      .then(checkStatus)
      .then(parseJSON)
      .then(check)
      .then(cb)
  )
})

export
const downloadProfile = createAction('_', (profileTemplateId, cb) => {
  return (
    fetch(`/onboarding-flows/profile-templates-csv-files/${profileTemplateId}`, {
      'method': 'get',
      'headers': {
       ...getToken(),
      }
    })
  )
})

export
const updateDocument = createAction('_', (documentId, arg, cb) =>
  fetch(`/documents/${documentId}`, { 
    ...putJson,
    ...body(arg)
  })
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
    .then(cb)
)

export
const resendInvitation = createAction('_', (userId, cb) =>
  fetch(`/onboarding-flows/hr/${userId}/resend-invitation-email`, getJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
    .then(cb)
)

export
const delStaff = createAction('_', (userId, cb) =>
  fetch(`/employ-managements/user-accounts/${userId}`, delJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
    .then(cb)
)

export
const fetchOnboardingUnhandleTasks = createAction('fetch onboarding unhandled tasks', () =>
  fetch('/onboarding-flows/hr/unhandled-tasks' ,getJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
    .then(data)
)

export 
const resetOnboardingFlows = createAction('reset onboarding flows');

export
const fetchOnboardingImportedUsers = createAction('fetch onboarding imported users', (args, cb) =>
  fetch(`/onboarding-flows/hr/imported-users?${util.body(args)}` ,getJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
    .then(res => {
      cb && cb();
      return res.data
    })
)

export
const reInviteImportedUser = createAction('_', (userId, cb) =>
  fetch(`/onboarding-flows/hr/imported-users/${userId}/resend-init-pwd-email` ,getJson)
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
)

export
const addOldUser = createAction('_', (arg, cb) =>
  fetch(`/onboarding-flows/hr/staff`,{
    ...postJson,
    ...body(arg),
  })
    .then(checkStatus)
    .then(parseJSON)
    .then(check)
    .then(cb)
)
